package com.zidian.leader.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zidian.leader.R;
import com.zidian.leader.activity.AttendanceRateCollegeActivity;
import com.zidian.leader.adapter.StudentStatisticsAdapter;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.fragment.BaseFragment;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.StudentStatisticsBean;
import com.zidian.leader.entity.StudentStatistics;
import com.zidian.leader.entity.StudentStatisticsCollege;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 出勤率Fragment
 * Created by GongCheng on 2017/2/15.
 */

public class AttendanceRateFragment extends BaseFragment {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private StudentStatisticsAdapter adapter;

    public static AttendanceRateFragment newInstance() {

        Bundle args = new Bundle();

        AttendanceRateFragment fragment = new AttendanceRateFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_recycler_view;
    }

    @Override
    protected void initView() {
        adapter = new StudentStatisticsAdapter(getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        //按院长/校长权限修改界面
        if (HawkUtils.getRoleId() == 6) {
            getStudentStatisticsCollege();
        } else {
            getStudentStatistics();
            adapter.setOnItemClickListener(new StudentStatisticsAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(String college, int sum, int num) {
                    Intent intent = new Intent(getContext(), AttendanceRateCollegeActivity.class);
                    intent.putExtra("college", college);
                    intent.putExtra("sum", sum);
                    intent.putExtra("num", num);
                    startActivity(intent);
                }
            });
        }

    }

    private void getStudentStatistics() {
        subscription = HttpFactory.getInstance().getStudentStatistics(HawkUtils.getUsername(),
                StringConstants.STRING_EMPTY,
                HawkUtils.getRoleId() + StringConstants.STRING_EMPTY,
                HawkUtils.getToken())
                .map(new Func1<StudentStatistics, List<StudentStatisticsBean>>() {
                    @Override
                    public List<StudentStatisticsBean> call(StudentStatistics colleges) {
                        List<StudentStatisticsBean> list = new ArrayList<StudentStatisticsBean>();
                        list.add(new StudentStatisticsBean(
                                "全校平均课程出勤率",
                                colleges.getSchool().getNumSum(),
                                colleges.getSchool().getNumC()));
                        for (int i = 0; i < colleges.getColleges().size(); i++) {
                            if (colleges.getColleges().get(i).getCollege().getNumSum() != 0) {
                                StudentStatisticsBean bean = new StudentStatisticsBean(
                                        colleges.getColleges().get(i).getCollegeName(),
                                        colleges.getColleges().get(i).getCollege().getNumSum(),
                                        colleges.getColleges().get(i).getCollege().getNumC());
                                list.add(bean);
                            }
                        }
                        return list;
                    }
                }).subscribe(new Subscriber<List<StudentStatisticsBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.dismiss();
                        Snackbar.make(recyclerView, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<StudentStatisticsBean> list) {
                        adapter.setDate(list);
                    }
                });
    }

    private void getStudentStatisticsCollege() {
        subscription = HttpFactory.getInstance().getStudentStatisticsCollege(
                HawkUtils.getUsername(),
                HawkUtils.getColleges().get(0).getCollege().getCollegeName(),
                HawkUtils.getRoleId() + StringConstants.STRING_EMPTY,
                HawkUtils.getToken())
                .map(new Func1<List<StudentStatisticsCollege>, List<StudentStatisticsBean>>() {
                    @Override
                    public List<StudentStatisticsBean> call(List<StudentStatisticsCollege> majors) {
                        List<StudentStatisticsBean> list = new ArrayList<StudentStatisticsBean>();
                        for (int i = 0; i < majors.size(); i++) {
                            if (majors.get(i).getMajor().getNumSum() != 0) {
                                list.add(new StudentStatisticsBean(
                                        majors.get(i).getMajorName(),
                                        majors.get(i).getMajor().getNumSum(),
                                        majors.get(i).getMajor().getNumC()));
                            }
                        }

                        return list;
                    }
                }).subscribe(new Subscriber<List<StudentStatisticsBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.dismiss();
                        Snackbar.make(recyclerView, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<StudentStatisticsBean> list) {
                        adapter.setDate(list);
                    }
                });
    }
}
